import java.io.*;


public class Printer {
    String tab = "    ";
    PrintWriter pw = null;


    public static void main(String[] args) throws Exception {
        StringBuilder sb = new StringBuilder("");
        BufferedReader br = new BufferedReader(new FileReader("src/input.in"));
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        Element e = new Element();
        e.parseString(sb.toString());        // now the root is 'e'.
        System.out.println(e.getName());

        Parser rd = new Parser(e);
        rd.start("src/Printer.out");


    }
    public void start() throws Exception {
//        dsdtree = schema(this.root);

//        Printer pp = new Printer();
//        pp.print(dsdtree, "printout.txt");

    }
    public void print(Node syntaxTree, String outFile) throws FileNotFoundException,IOException {
        this.pw = new PrintWriter(new FileWriter(outFile,true));
        System.out.println("pretty printer output:");
        recursivePrint(syntaxTree, 0);
        pw.flush();
        pw.close();
    }

    
    private void recursivePrint(Node current, int level) {
        StringBuilder sb = new StringBuilder("");
        StringBuilder indent = new StringBuilder("");

        for (int i = 0; i < level; i++) {
            indent.append(tab);
        }
        sb.append(indent.toString());
//        if (current.children.size() > 0) {
//             sb.append("+");                // expanded.
//        }  else if (current.inline) {
//            sb.append("-");               // no children at all.....
//        }  else {
//            sb.append("*");
//        }
        sb.append("<");
        sb.append(current.type);
        int size = 0;
        if ((size = current.attributes.size()) > 0) {
//            sb.append("   [");
            sb.append(" ");
            int i = 0;
            for (Attribute a : current.attributes) {
                sb.append(a.type);
                sb.append("=\"");
                sb.append(a.value);
                sb.append("\"");
                i++;
                if (i < size) {
                    sb.append(" ");
                }
            }
//            sb.append(">");
        }

        if (current.inline) {
            sb.append(" /");
        }
        sb.append(">");

        pw.println(sb.toString());
        System.out.println(sb.toString());
        if (!current.inline) {
            for (Node child : current.children) {
                recursivePrint(child, level + 1);
            }
            sb = new StringBuilder(indent.toString() + "</" + current.type + ">");
            pw.println(sb.toString());
            System.out.println(sb.toString());
        }
    }
}
